﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AdminUser : System.Web.UI.Page
{
    ManageDatabase data;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.IsAuthenticated)
        {
            if (data == null)
            {
                data = new ManageDatabase();
            }
            if (User.IsInRole("admin"))
            {
                if (!IsPostBack)
                {
                    Initialize();
                }
                else {
                    GetContent(Convert.ToInt32(AdminTypeDropDownList.SelectedValue), AdminStatusDropDownList.SelectedValue);
                }
            }
            else
            {
                Response.Redirect("~/Default.aspx");
            }
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }

    }

    private void Initialize()
    {

        AdminTypeDropDownList.Items.Add(new ListItem("--Összes--", "0"));
        AdminTypeDropDownList.Items.Add(new ListItem("Admin", "1"));
        AdminTypeDropDownList.Items.Add(new ListItem("Felhasználó", "2"));

        AdminStatusDropDownList.Items.Add(new ListItem("--Összes--", ""));
        AdminStatusDropDownList.Items.Add(new ListItem("Engedélyezett felhasználók", "1"));
        AdminStatusDropDownList.Items.Add(new ListItem("Letiltott felhasználók", "-1"));
        AdminStatusDropDownList.Items.Add(new ListItem("Új felhasználók", "0"));

        GetContent(0, "");
    }

    protected void AdminList_Click(object sender, EventArgs e)
    {
        AdminResultPanel.Controls.Clear();
        GetContent(Convert.ToInt32(AdminTypeDropDownList.SelectedValue), AdminStatusDropDownList.SelectedValue);
    }

    private void GetContent(int type, string status)
    {
        data = new ManageDatabase();
        bool admin = true;
        bool user = true;
        bool statusFlag = false;
        if (type == 1)
        {
            admin = false;
            user = true;
        }
        else if (type == 2)
        {
            admin = true;
            user = false;
        }
        int statusInt = 0;
        if (status != "")
        {
            statusFlag = true;
            statusInt = Convert.ToInt32(status);
        }

        List<User> result = data.SearchAdminListUser(admin, user, statusInt, statusFlag);

        Label ResultText = new Label();
        Table ResultTable = new Table();
        ResultTable.CssClass = "gradient-style";

        if (result != null)
        {
            TableHeaderRow ResultHeaderRow = new TableHeaderRow();
            TableHeaderCell ResultHeaderCell1 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell2 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell3 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell4 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell5 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell6 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell7 = new TableHeaderCell();
            TableHeaderCell ResultHeaderCell8 = new TableHeaderCell();
            Label Cell1 = new Label();
            Label Cell2 = new Label();
            Label Cell3 = new Label();
            Label Cell4 = new Label();
            Label Cell5 = new Label();
            Label Cell6 = new Label();
            Label Cell7 = new Label();
            Label Cell8 = new Label();
            Cell1.Text = "#  ";
            Cell2.Text = "Felhasználónév";
            Cell3.Text = "E-mail";
            Cell4.Text = "Típus";
            Cell5.Text = "Státusz";
            Cell6.Text = "Regisztráció dátuma";
            Cell7.Text = "Státusz";
            Cell8.Text = "Törlés";
            ResultHeaderCell1.Controls.Add(Cell1);
            ResultHeaderCell2.Controls.Add(Cell2);
            ResultHeaderCell3.Controls.Add(Cell3);
            ResultHeaderCell4.Controls.Add(Cell4);
            ResultHeaderCell5.Controls.Add(Cell5);
            ResultHeaderCell6.Controls.Add(Cell6);
            ResultHeaderCell7.Controls.Add(Cell7);
            ResultHeaderCell8.Controls.Add(Cell8);
            ResultHeaderRow.Controls.Add(ResultHeaderCell1);
            ResultHeaderRow.Controls.Add(ResultHeaderCell2);
            ResultHeaderRow.Controls.Add(ResultHeaderCell3);
            ResultHeaderRow.Controls.Add(ResultHeaderCell4);
            ResultHeaderRow.Controls.Add(ResultHeaderCell5);
            ResultHeaderRow.Controls.Add(ResultHeaderCell6);
            ResultHeaderRow.Controls.Add(ResultHeaderCell7);
            ResultHeaderRow.Controls.Add(ResultHeaderCell8);
            ResultTable.Controls.Add(ResultHeaderRow);

            int i = 0;
            foreach (User item in result)
            {
                TableRow InfoRow = new TableRow();
                TableCell Info1Cell = new TableCell();
                TableCell Info2Cell = new TableCell();
                TableCell Info3Cell = new TableCell();
                TableCell Info4Cell = new TableCell();
                TableCell Info5Cell = new TableCell();
                TableCell Info6Cell = new TableCell();
                TableCell Info7Cell = new TableCell();
                TableCell Info8Cell = new TableCell();

                Info7Cell.HorizontalAlign = HorizontalAlign.Center;
                Info8Cell.HorizontalAlign = HorizontalAlign.Center;

                Label Count = new Label();
                Label UserName = new Label();
                Label Email = new Label();
                Label Type = new Label();
                Label Status = new Label();
                Label RegDate = new Label();
                ImageButton EditStatus = new ImageButton();
                ImageButton Delete = new ImageButton();

                i++;
                Count.Text = i.ToString() + ". ";

                UserName.Text = item.username;
                Email.Text = item.email;
                Type.Text = item.role;
                if (item.status == 1)
                    Status.Text = "Engedélyezett";
                else if (item.status == -1)
                    Status.Text = "Letiltott";
                else if (item.status == 0)
                    Status.Text = "Új";

                RegDate.Text = String.Format("{0:yyyy-MM-dd HH:mm:ss}", item.regDate);
                EditStatus.ID = "e" + item.id.ToString();
                if (item.status == 1)
                {
                    EditStatus.ToolTip = "Letilt";
                    EditStatus.ImageUrl="~/images/icons/minus-icon.png";
                }
                else
                {
                    EditStatus.ToolTip = "Engedélyez";
                    EditStatus.ImageUrl="~/images/icons/plus-icon.png";
                    
                }
                EditStatus.Click += new ImageClickEventHandler(EditStatus_Click);

                Delete.ID = "d" + item.id.ToString();
                Delete.ToolTip = "Törlés";
                Delete.ImageUrl = "~/images/icons/delete-icon.png";
                Delete.OnClientClick = "return confirm('Biztos törli a felhasználót?');";
                Delete.CommandName = "Delete";
                Delete.Click += new ImageClickEventHandler(Delete_Click);

                Info1Cell.Controls.Add(Count);
                Info2Cell.Controls.Add(UserName);
                Info3Cell.Controls.Add(Email);
                Info4Cell.Controls.Add(Status);
                Info5Cell.Controls.Add(Type);
                Info6Cell.Controls.Add(RegDate);
                Info7Cell.Controls.Add(EditStatus);
                Info8Cell.Controls.Add(Delete);


                InfoRow.Controls.Add(Info1Cell);
                InfoRow.Controls.Add(Info2Cell);
                InfoRow.Controls.Add(Info3Cell);
                InfoRow.Controls.Add(Info4Cell);
                InfoRow.Controls.Add(Info5Cell);
                InfoRow.Controls.Add(Info6Cell);
                InfoRow.Controls.Add(Info7Cell);
                InfoRow.Controls.Add(Info8Cell);

                ResultTable.Controls.Add(InfoRow);
                
            }
        }

        AdminResultPanel.Controls.Add(ResultTable);
    }

    protected void EditStatus_Click(object sender, ImageClickEventArgs e)
    {
        data = new ManageDatabase();

        string id = ((ImageButton)sender).ID;
        int user_id = Convert.ToInt32(id.Substring(1));

        int user_status = data.GetUserStatusById(user_id);
        Boolean success  = false;
        if(user_status == 1)
            success = data.UpdateUserStatus(user_id, -1);
        else if (user_status == -1 || user_status == 0)
            success = data.UpdateUserStatus(user_id, 1);
        else
            success = false;

        if (success) {
            AdminResultPanel.Controls.Clear();
            GetContent(Convert.ToInt32(AdminTypeDropDownList.SelectedValue), AdminStatusDropDownList.SelectedValue);
        }
        else {
            Alert.Show("A státuszt nem sikerült módosítani!");
        }
    }

    protected void Delete_Click(object sender, ImageClickEventArgs e)
    {
        data = new ManageDatabase();

        string id = ((ImageButton)sender).ID;
        int user_id = Convert.ToInt32(id.Substring(1));

        Boolean success = data.DeleteUser(user_id);

        if (success) {
            AdminResultPanel.Controls.Clear();
            GetContent(Convert.ToInt32(AdminTypeDropDownList.SelectedValue), AdminStatusDropDownList.SelectedValue);
        }
        else {
            Alert.Show("A felhasználót nem sikerült törölni!");
        }
    }
}